/*
 * Create:      柿子
 * CreateDate:  2022/12/10
 * Describe：   查看验光单 / 查看已写过的病历
 * */
import React, { Component } from 'react';
import { Modal, Empty, Spin } from 'antd';
import ElectronicCaseTemplate from 'pages/common/ElectronicCaseTemplate.jsx';

class ViewWrittenMedicalRecords extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      rowData: {},
      loading: false,
      emrLoadingFlag: true, // 是否加载病历
    };
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  }

  // 修改弹窗状态
  modifyVisible = (visible, rowData) => {
    let emrLoadingFlag = rowData && 'admID' in rowData && rowData.admID && 'patEMRRecordID' in rowData && rowData.patEMRRecordID;
    this.setState({ visible, rowData, emrLoadingFlag }, () => {
      emrLoadingFlag && visible && this.handleLoadingMedicalRecords();
    });
  }

  // 病历加载
  handleLoadingMedicalRecords = async () => {
    try {
      this.setState({ loading: true });
      const { rowData } = this.state;
      let data = {
        params: [{
          patEMRRecordID: rowData?.patEMRRecordID || '',
          admID: rowData?.admID || '',
          power: ''
        }]
      }
      let res = await React.$asyncPost(this, '06050008', data);
      this.setState({ loading: false });
      let templatePath = res.result?.templatePath || '';
      templatePath ? this.caseTemplateChild && this.caseTemplateChild.setElementTextByID(
        templatePath,
        res.result?.dataSource || [],
        'N',
        "boolFalse",
        rowData?.patSexDesc || '',
      ) : this.caseTemplateChild && this.caseTemplateChild?.NewFile();
    } catch (error) {
      this.setState({ loading: false });
    }
  }

  render() {
    const { title, editorID } = this.props;
    const { visible, emrLoadingFlag, loading } = this.state;
    let param = {
      isDelete: true,
      formView: 'Normal',
      isBasicOperation: true,
      prCont: false,
      isFont: true,
      isInsert: 'hidden',
    }
    
    return (
      <Modal
        width="56vw"
        title={title || '查看病历'}
        footer={null}
        visible={visible}
        forceRender={true}
        bodyStyle={{ padding: '12px' }}
        onCancel={() => this.modifyVisible(false)}
      >
        <Spin tip="数据加载中，请稍后……" spinning={loading}>
          <div style={{ minHeight: '450px' }}>
            <div style={{ height: '72vh', display: emrLoadingFlag ? 'block' : 'none' }}>
              <ElectronicCaseTemplate
                param={param}
                editorID={editorID || 'ViewWrittenMedicalRecords'}
                onRef={ref => this.caseTemplateChild = ref}
              />
            </div>
            {!emrLoadingFlag ? <Empty style={{ marginTop: 200 }} description="暂无数据" /> : ''}
          </div>
        </Spin>
      </Modal>
    )
  }
};

export default ViewWrittenMedicalRecords;